#include "odn_scene_Camera.h"

namespace ouden
{
	namespace scene
	{
	
		//public:
		
		//Update the motion of the camera
		//Called by scene
		virtual bool ODN_Camera::Update()
		{
			
			//Check that we need to do anything first
			if (!needtomove) return true;
			
			//Find vector between current scroll position and destination
			Vector3D between = destination - scroll;
			
			//Are we already there?
			needtomove = !between.isZero();
			
			//Check that we're not already there
			if (needtomove) {
				
				updateSpeed();
				
				//Integrate the scroll position according to the scroll speed in the direction of the between vector 
				scroll += scrollspeed * ouden::getEngine()->getTimeManager()->getElapsed() * between.UnitVector();
				
			}
			else {
				
				//If we've reached the destination then reset the scroll speed
				scrollspeed = 0;	
				
			}
			
		}
		
		//Scrolls the camera to center over the desired position 
		void ODN_Camera::lookAt(Vector3D Pos)
		{
			//Set the new destination
			destination = Pos;
			//We need to move to get there now 
			needtomove = true;
		}
		
		//Centers the camera over the desired position 
		void ODN_Camera::lookAtNow(Vector3D Pos)
		{
			//Jump straight to the position and center
			scroll = Pos - viewsize;
			//Reset the scroll speed
			scrollspeed = 0;
			//Don't need to move now
			needtomove = false;	
		}
		
		//private:
		
		//Accelerate the scroll speed
		void ODN_Camera::updateSpeed()
		{
			//Integrate the scroll speed according to the given acceleration
			scrollspeed += scrollaccel * ouden::getEngine()->getTimeManager()->getElapsed();
			
			//Limit the speed to maxscrollspeed
			if (scrollspeed > maxscrollspeed) scrollspeed = maxscrollspeed;	
		}
		
	} //namespace scene
} //namwspace ouden
